package com.handinfo.uimenu.window;

import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Graphics;

import com.handinfo.engine.AbstractGameScreen;
import com.handinfo.engine.GameManager;
import com.handinfo.game.ActorRole;
import com.handinfo.uimenu.UIAbstractWindow;
import com.handinfo.uimenu.UIDialogMenu;
import com.handinfo.uimenu.UIListener;
import com.handinfo.uimenu.UIManager;
import com.handinfo.uimenu.UIMessageBox;
import com.handinfo.uimenu.UIWindowFactory;
import com.handinfo.util.Tools;

public class UIBattlefieldWindow extends UIAbstractWindow
{

	public UIBattlefieldWindow(int id) {
		super(id);
	}

	protected void paintContent(Graphics g) {
		
	}

	protected void keyLogic() {
		
	}
	/**
	 * 读取战场列表
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldList(DataInputStream dis) throws IOException {
		byte size = dis.readByte();
		Tools.debugPrintln("战场数量 : " + size);
		if (size > 0) {
			keys = new String[size];
			names = new String[size];
			
			for (byte i = 0; i < size; i++) {
				keys [i]= dis.readUTF();
				names[i] = dis.readUTF() + "(限制等级" + dis.readInt() + ")";
				Tools.debugPrintln("第" + i + "个战场key : " + keys[i] + ",name : " + names[i]);
			}
			UIManager.getInstance().removeWindow(GameManager.getInstance().m_npc_window.m_win_npc_list);
			final UIDialogMenu dm = (UIDialogMenu)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MENU);
			dm.setContents("战场列表", "请选择加入的战场!", names);
			dm.addListener(new UIListener() {
				public void actionPerformed() {
					if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
							|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
						int index = dm.getCurrentIndex();
						GameManager.getInstance().m_game_message.sendBattlefieldJoinQueue(keys[index], (byte)1, GameManager.getInstance().m_role.m_gameID);
						UIManager.getInstance().removeWindow(dm);
					} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
						UIManager.getInstance().removeWindow(dm);
					}
				}
			});
		}
	}
	/**
	 * 读取加入队列返回信息
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldJoinQueue(DataInputStream dis) throws IOException {
		byte status = dis.readByte();
		if (status == 0) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, dis.readUTF());
		} else {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, "申请成功," + dis.readUTF() + "战场开启!");
		}
	}
	/**
	 * 读取询问是否进入战场
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldconfirmationInto(DataInputStream dis) throws IOException {
		String name = dis.readUTF();
		final UIMessageBox mb = (UIMessageBox)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		mb.setMessage("是否进入" + name + "战场!");
		mb.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)) {
					GameManager.getInstance().m_game_message.sendBattlefieldInto();
					UIManager.getInstance().removeWindow(mb);
				} else if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_RIGHT_SOFT)) {
					GameManager.getInstance().m_game_message.sendBattlefieldExit();
					UIManager.getInstance().removeWindow(mb);
				}
			}
		});
	}
	/**
	 * 读取进入战场
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldInto(DataInputStream dis) throws IOException {
		short status = dis.readShort();
		if (status == 0) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, dis.readUTF());
		}
	}
	/**
	 * 读取离开战场
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldExit(DataInputStream dis) throws IOException {
		short status = dis.readShort();
		if (status == 0) {
			GameManager.getInstance().addSystemInfo(GameManager.SYSTEM_INFO_TYPE_A, dis.readUTF());
		}
	}
	/**
	 * 读取战场死亡窗口
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldDead(DataInputStream dis) throws IOException {
		m_dead_show = false;
		m_killer_name = dis.readUTF();
		m_dead_second = dis.readByte() * GameManager.FRAME_PER_SECOND;
		umb = null;
		umb = (UIMessageBox)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		umb.setMessage("战场提示", null, null, DEAD_WORD_1 + m_killer_name + DEAD_WORD_2 + (m_dead_second / 10) + DEAD_WORD_3);
		umb.addListener(new UIListener() {
			public void actionPerformed() {
				if (m_dead_show) {
					m_dead_second--;
					if (umb != null) {
						if (m_dead_second <= 0) {
							m_dead_show = false;
							m_dead_second = 0;
							UIManager.getInstance().removeWindow(umb);
						} else {
							umb.setMessage("战场提示", null, null, DEAD_WORD_1 + m_killer_name + DEAD_WORD_2 + (m_dead_second / 10) + DEAD_WORD_3);
						}
					}
				}
			}
		});
		m_dead_show = true;
	}
	/**
	 * 读取战场结束
	 * @param dis
	 * @throws IOException
	 */
	public static void recvBattlefieldFinish(DataInputStream dis) throws IOException {
		GameManager.getInstance().m_role.closeBattlefieldScore();
		UIManager.getInstance().removeOtherWindows();
		
		String result = dis.readByte() == 0 ? "失败" : "成功";
		byte key = dis.readByte();
		String win_faction = key == 0 ? "无" : ActorRole.FACTION_WORD[key];
		byte size = dis.readByte();
		String text = "";
		for (int i = 0; i < size; i++) {
			text += ActorRole.ROLE_FACTION_LIST[dis.readByte()] + "阵营得分 : " + dis.readInt() + Tools.CHANGE_ROW;
		}
		int score = dis.readInt();
		byte exploit = dis.readByte();
		umb = null;
		umb = (UIMessageBox)UIManager.getInstance().createWindow(UIWindowFactory.WINDOW_MESSAGEBOX);
		umb.setMessage(result, "确定", null,
				win_faction + "阵营获胜!" + Tools.CHANGE_ROW + text
				+ "您的得分 : " + score + Tools.CHANGE_ROW
				+ "您的功勋 : " + exploit + Tools.CHANGE_ROW);
		umb.addListener(new UIListener() {
			public void actionPerformed() {
				if (AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_A)
						|| AbstractGameScreen.haveKeyPressed(AbstractGameScreen.GK_LEFT_SOFT)) {
					UIManager.getInstance().removeWindow(umb);
				}
			}
		});
	}
	private static String m_killer_name = null;
	private static int m_dead_second = 0;
	private static final String DEAD_WORD_1 = "你被";
	private static final String DEAD_WORD_2 = "杀死,你将在";
	private static final String DEAD_WORD_3 = "秒后自动复活!";
	private static boolean m_dead_show = false;//是否显示死亡窗口
	private static UIMessageBox umb = null;
	
	private static String[] keys = null;
	private static String[] names = null;
}
